package com.haier.npt;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
import org.springframework.cloud.netflix.zuul.filters.discovery.PatternServiceRouteMapper;
import org.springframework.context.annotation.Bean;

import com.haier.npt.filter.IgnoreUrlFilter;
import com.haier.npt.filter.authentication.UserTokenAuthenticationFilter;
import com.haier.npt.filter.authorization.UserTokenAuthorizationFilter;
import com.netflix.zuul.ZuulFilter;

/**
 * @Description:网关启动类
 * @author: lixu
 * @date:   2018年7月12日 上午10:28:36   
 */
@EnableZuulProxy
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
@ServletComponentScan
public class GateWayApplication {
	public static void main(String[] args){
		SpringApplication.run(GateWayApplication.class, args);
	}
	
	@Bean
	public PatternServiceRouteMapper serviceRouteMapper(){
		return new PatternServiceRouteMapper("(?<name>^.+)-(?<version>v.+$)", "${name}/${version}");
	}
	
	/**
	 * @Description: 忽略url 拦截访问的url
	 * @param: @return      
	 * @return: ZuulFilter      
	 * @throws   
	 */
	@Bean
	public ZuulFilter ignoreFilter(){
		return new IgnoreUrlFilter();
	}
	
	/**
	 * @Description: 认证filter
	 * @param: @return      
	 * @return: ZuulFilter      
	 * @throws   
	 */
	@Bean
	public ZuulFilter authenticationFilter(){
		return new UserTokenAuthenticationFilter();
	}
	
	/**
	 * 权限filter
	 * @return
	 */
	@Bean
	@ConditionalOnProperty(prefix="authority",name="enable",havingValue="true")
	public ZuulFilter authorizationFilter(){
		return new UserTokenAuthorizationFilter();
	}

}
